package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTradeMarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTradeMarkMapper baseTradeMarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //使用base_category_trademark表   查出base_trademark的trademark_id的ID
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> ids = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            List<BaseTrademark> baseTrademarkList = baseTradeMarkMapper.selectBatchIds(ids);
            return baseTrademarkList;
        }
        return null;
    }
    /**
     * 删除分类品牌关联
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void remove(Long category3Id, long trademarkId) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        queryWrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(queryWrapper);
    }

    /**
     * 根据category3Id获取可选品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
    //先把base_category_trademark表中的已经被显示的id拿出来
        List<BaseTrademark> baseTrademarkList = this.findTrademarkList(category3Id);
        if (!CollectionUtils.isEmpty(baseTrademarkList)){
            List<Long> selectedNumber = baseTrademarkList.stream().map(baseTrademark -> baseTrademark.getId()).collect(Collectors.toList());

            //拿出商品表中一共有几种
            List<BaseTrademark> totalBaseTrademarkList = baseTradeMarkMapper.selectList(null);
            //下面stream做一个筛选   他们的差集
            List<BaseTrademark> lastTrademarkList = totalBaseTrademarkList.stream().filter(baseTrademark -> {
                return !selectedNumber.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
//            List<Long> totalNumber = totalBaseTrademarkList.stream().map(baseTrademark -> baseTrademark.getId()).collect(Collectors.toList());
//            boolean b = !selectedNumber.contains(totalNumber);
            return lastTrademarkList;
        }
        //如果页面没有已经显示的元素，就直接全部显示
        return baseTradeMarkMapper.selectList(null);
    }
    /**
     * 保存分类品牌关联
     */
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            List<BaseCategoryTrademark> collect = trademarkIdList.stream().map((tmId) -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tmId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }





}
